package limeirafol.gui.sistema;

import lbframework.bean.internalFrame.LBInternalFrame;
import lbframework.bean.mdiFrame.LBMdiFrame;
import lbframework.classe.Util;
import limeirafol.Global;
import limeirafol.bean.BeanSistema;
import limeirafol.remote.Formulario;
import limeirafol.remote.TipoOperacao;
import limeirafol.remote.sistema.EmailVO;

public class EmailConfiguracaoGUI extends LBInternalFrame {

    private BeanSistema beanSistema = new BeanSistema();
    private EmailVO oEmail = null;

    public EmailConfiguracaoGUI(LBMdiFrame i_MdiFrame) throws Exception {
        super(i_MdiFrame);
        initComponents();

        centralizarForm();

        carregarEmail();
    }

    @Override
    public void salvar() throws Exception {
        oEmail.idUsuario = Global.idUsuario;
        oEmail.email = txtEmail.getText();
        oEmail.senha = txtSenha.getText();
        oEmail.servidor = txtServidor.getText();
        oEmail.porta = txtPorta.getInt();
        oEmail.requerAutenticacao = chkRequerAutenticacao.isSelected();
        oEmail.tls = chkTls.isSelected();

        beanSistema.salvarEmail(oEmail);

        Util.exibirMensagem(Util.MSG_SALVO_SUCESSO, getTitle());
    }

    private void carregarEmail() throws Exception {
        oEmail = beanSistema.carregarEmail(Global.idUsuario);

        txtEmail.setText(oEmail.email);
        txtSenha.setText(oEmail.senha);
        txtServidor.setText(oEmail.servidor);
        txtPorta.setText(oEmail.porta > 0 ? String.valueOf(oEmail.porta) : "");
        chkRequerAutenticacao.setSelected(oEmail.requerAutenticacao);
        chkTls.setSelected(oEmail.tls);
    }

    private void testarConexaoEmail() throws Exception {
        Util.testarConexaoEmail(txtEmail.getText(), txtSenha.getText(), txtServidor.getText(), chkRequerAutenticacao.isSelected(), chkTls.isSelected(), txtPorta.getInt());

        Util.exibirMensagem("Conectado com sucesso!", getTitle());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lBPanel1 = new lbframework.bean.panel.LBPanel();
        lBLabel1 = new lbframework.bean.label.LBLabel();
        txtEmail = new lbframework.bean.textField.LBTextField();
        lBLabel2 = new lbframework.bean.label.LBLabel();
        lBLabel3 = new lbframework.bean.label.LBLabel();
        txtServidor = new lbframework.bean.textField.LBTextField();
        lBLabel4 = new lbframework.bean.label.LBLabel();
        txtPorta = new lbframework.bean.textField.LBTextField();
        chkRequerAutenticacao = new lbframework.bean.checkBox.LBCheckBox();
        chkTls = new lbframework.bean.checkBox.LBCheckBox();
        txtSenha = new lbframework.bean.passwordField.LBPasswordField();
        lBToolBar1 = new lbframework.bean.toolBar.LBToolBar();
        lBToolBarPadrao1 = new lbframework.bean.toolBarPadrao.LBToolBarPadrao();
        btnTestarConexaoEmail = new lbframework.bean.button.LBButton();
        lBPanel2 = new lbframework.bean.panel.LBPanel();
        btnSair = new lbframework.bean.button.LBButton();
        btnSalvar = new lbframework.bean.button.LBButton();

        setTitle("Configuração de Email");

        lBLabel1.setText("Endereço de Email");

        txtEmail.setCaixaAlta(false);

        lBLabel2.setText("Senha");

        lBLabel3.setText("Servidor de saída (SMTP)");

        txtServidor.setCaixaAlta(false);

        lBLabel4.setText("Porta");

        txtPorta.setCaixaAlta(false);

        chkRequerAutenticacao.setText("Requer autenticação");

        chkTls.setText("Requer uma conexão segura (TLS/SSL)");

        txtSenha.setCaixaAlta(false);

        javax.swing.GroupLayout lBPanel1Layout = new javax.swing.GroupLayout(lBPanel1);
        lBPanel1.setLayout(lBPanel1Layout);
        lBPanel1Layout.setHorizontalGroup(
            lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lBPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(lBPanel1Layout.createSequentialGroup()
                        .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lBLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtServidor, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPorta, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lBLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chkTls, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(chkRequerAutenticacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(lBPanel1Layout.createSequentialGroup()
                        .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lBLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lBLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
        );
        lBPanel1Layout.setVerticalGroup(
            lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lBPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(lBPanel1Layout.createSequentialGroup()
                        .addComponent(lBLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26))
                    .addGroup(lBPanel1Layout.createSequentialGroup()
                        .addComponent(lBLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(lBPanel1Layout.createSequentialGroup()
                        .addComponent(lBLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtServidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(lBPanel1Layout.createSequentialGroup()
                        .addComponent(lBLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPorta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(lBPanel1Layout.createSequentialGroup()
                        .addComponent(chkRequerAutenticacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chkTls, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lBToolBar1.setRollover(true);

        lBToolBarPadrao1.setRollover(true);
        lBToolBarPadrao1.setVisibleSalvar(true);
        lBToolBar1.add(lBToolBarPadrao1);

        btnTestarConexaoEmail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/emailtestarconexao.png"))); // NOI18N
        btnTestarConexaoEmail.setToolTipText("Testar Conexão");
        btnTestarConexaoEmail.setFocusable(false);
        btnTestarConexaoEmail.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnTestarConexaoEmail.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnTestarConexaoEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTestarConexaoEmailActionPerformed(evt);
            }
        });
        lBToolBar1.add(btnTestarConexaoEmail);

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/sair.png"))); // NOI18N
        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/salvar.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout lBPanel2Layout = new javax.swing.GroupLayout(lBPanel2);
        lBPanel2.setLayout(lBPanel2Layout);
        lBPanel2Layout.setHorizontalGroup(
            lBPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, lBPanel2Layout.createSequentialGroup()
                .addContainerGap(267, Short.MAX_VALUE)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        lBPanel2Layout.setVerticalGroup(
            lBPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lBPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lBToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lBPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lBPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lBToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lBPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lBPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnTestarConexaoEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTestarConexaoEmailActionPerformed
        try {
            testarConexaoEmail();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_btnTestarConexaoEmailActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        try {
            salvar();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        try {
            setClosed(true);

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_btnSairActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton btnSair;
    private lbframework.bean.button.LBButton btnSalvar;
    private lbframework.bean.button.LBButton btnTestarConexaoEmail;
    private lbframework.bean.checkBox.LBCheckBox chkRequerAutenticacao;
    private lbframework.bean.checkBox.LBCheckBox chkTls;
    private lbframework.bean.label.LBLabel lBLabel1;
    private lbframework.bean.label.LBLabel lBLabel2;
    private lbframework.bean.label.LBLabel lBLabel3;
    private lbframework.bean.label.LBLabel lBLabel4;
    private lbframework.bean.panel.LBPanel lBPanel1;
    private lbframework.bean.panel.LBPanel lBPanel2;
    private lbframework.bean.toolBar.LBToolBar lBToolBar1;
    private lbframework.bean.toolBarPadrao.LBToolBarPadrao lBToolBarPadrao1;
    private lbframework.bean.textField.LBTextField txtEmail;
    private lbframework.bean.textField.LBTextField txtPorta;
    private lbframework.bean.passwordField.LBPasswordField txtSenha;
    private lbframework.bean.textField.LBTextField txtServidor;
    // End of variables declaration//GEN-END:variables
}
